﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Model;
using Besture.Bll;
using Besture.Common.Definition;
using Besture.Utils;
using Besture.Common;
using System.Text.RegularExpressions;

namespace Besture.UI.Email
{
    public partial class Cipher : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSendMsg_Click(object sender, EventArgs e)
        {
            string checkCode = txtVailidCode.Value.Trim();
            if (checkCode.ToUpper() != Session["CheckCode"] + "")
            {
                this.Page.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>GetVailidCode();alert('验证码不正确');</script>");
                return;
            }
            string telphone = txtTelphone.Value.Trim();
            if (string.IsNullOrEmpty(telphone))
            {
                this.Page.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>GetVailidCode();alert('手机号码不能为空');</script>");
                return;
            }
            if (!Regex.IsMatch(telphone, AppConfig.SMSRegex))
            {
                this.Page.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>GetVailidCode();alert('手机号码格式不正确');</script>");
                return;
            }
            var Code = Fun.GetCode();
            Session["SMSCode"] = Code;

            string flag = SendSMSUtil.SendSMS(telphone, string.Format(AppConfig.SMSCipherTemplate, Code));
            if (flag == "发送成功")
            {
                Session["CheckCode"] = null;
                Session[Constants.UserMobileCipherSessionKey] = telphone;
                Response.Redirect("Cipher1.aspx");
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>GetVailidCode();alert('发送短信失败，请重新发送');</script>");
                return;
            }
        }
    }
}